package Jackson.home.com.compilador;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;

import javax.tools.JavaCompiler;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;


public class Main {

	public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException {
		
		/*JavaCompiler jc = ToolProvider.getSystemJavaCompiler();
		StandardJavaFileManager sjfm = jc.getStandardFileManager(null, null, null); 
		
		
		File arquivo = new File("C:/Users/Jackson/workspace/Poo2Testes/arquivos/arquivo1.java");
		
		
		Iterable fileObjects = sjfm.getJavaFileObjects(arquivo);
		
		String [] options = new String[]{"-c","C:/Users/Jackson/workspace/Poo2Testes/bin"};
		
		
		jc.getTask(null, null, null, Arrays.asList(options), null, fileObjects).call();
		
		//jc.getTask(null, null, null, Arrays.asList(options), null, fileObjects).call();
		
		sjfm.close();
		
		System.out.println("Class has been successfully compiled");
		
		
		
		//Teste de existencia de método ou construtor
		URL[] urls = new URL[]{ new URL("file://arquivos/arquivo1.java")};
		
		URLClassLoader ucl = new URLClassLoader(urls);
		
		Class clazz = ucl.loadClass("arquivos/arquivo1.java");
		
		System.out.println("Class has been successfully loaded");
		

		Method method = clazz.getDeclaredMethod("imprimir", null);
		
		Object object = clazz.newInstance();
		method.invoke(object, null);
		
		System.out.println("Método encontrado!");
		*/
		
		System.setProperty("java.home", "C:/Program Files/Java/jdk1.7.0_40");
		
		
		String arquivo  = "C:\\Users\\Jackson\\workspace\\Poo2Testes\\src\\Jackson\\home\\com\\compilador\\arquivo1.java";
		
		
		JavaCompiler compilador = ToolProvider.getSystemJavaCompiler();
		
		int resultado = compilador.run(null, null, null, arquivo);
		
		if(resultado == 0){
			System.out.println("Compilação ocorrida com sucesso!");
		}else{
			System.out.println("Compilação falhou!");
		}
		
	}

	
}




